package com.dhnsoft.education.service.statistics.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.dhnsoft.education.entity.statistics.StatisticsDaily;
import com.dhnsoft.education.mapper.statistics.StatisticsDailyMapper;
import com.dhnsoft.education.service.statistics.StatisticsDailyService;
import com.dhnsoft.education.service.userCenter.UcenterMemberService;
import com.dhnsoft.education.utils.R;
import org.apache.commons.lang3.RandomUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.bind.annotation.PathVariable;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * <p>
 * 网站统计日数据 服务实现类
 * </p>
 *
 * @author 代浩楠
 * @since 2022-04-18
 */
@Service
public class StatisticsDailyServiceImpl extends ServiceImpl<StatisticsDailyMapper, StatisticsDaily> implements StatisticsDailyService {

    @Autowired
    private UcenterMemberService memberService;

    @Override
    public void registerCount(String day) {
        //添加之前删除表中相同日期的数据
        QueryWrapper<StatisticsDaily> wrapper = new QueryWrapper<>();
        wrapper.eq("date_calculated",day);
        baseMapper.delete(wrapper);
        //插入最新的
        R registerR = countRegister(day);
        Integer countRegister = (Integer) registerR.getData().get("countRegister");

        StatisticsDaily statisticsDaily = new StatisticsDaily();
        statisticsDaily.setDateCalculated(day);//统计日期
        statisticsDaily.setRegisterNum(countRegister);//统计注册数
        statisticsDaily.setLoginNum(RandomUtils.nextInt(100,200));// TODO 每日登录统计
        statisticsDaily.setCourseNum(RandomUtils.nextInt(100,200));// TODO 每日观看课程数统计
        statisticsDaily.setVideoViewNum(RandomUtils.nextInt(100,200));// TODO 每日观看视频数统计
        baseMapper.insert(statisticsDaily);
    }

    @Override
    public Map getShowData(String type, String begin, String end) {
        //根据条件查询对应数据
        QueryWrapper<StatisticsDaily> wrapper = new QueryWrapper<>();
        wrapper.between("date_calculated",begin,end);
        wrapper.select("date_calculated",type);
        List<StatisticsDaily> statisticsDailies = baseMapper.selectList(wrapper);
        //因为返回有两部分数据，日期和日期对应的数量
        //前端要求数组json数据
        //创建两个list集合，一个日期list，一个数量list
        List<String> date_calculatedList = new ArrayList<>();
        List<Integer> numDataList = new ArrayList<>();
        for (StatisticsDaily statisticsDaily : statisticsDailies) {
            date_calculatedList.add(statisticsDaily.getDateCalculated());
            switch (type){
                case "login_num":
                    numDataList.add(statisticsDaily.getLoginNum());
                    break;
                case "register_num":
                    numDataList.add(statisticsDaily.getRegisterNum());
                    break;
                case "video_view_num":
                    numDataList.add(statisticsDaily.getVideoViewNum());
                    break;
                case "course_num":
                    numDataList.add(statisticsDaily.getCourseNum());
                    break;
                default:
                    break;
            }
        }
        Map<String,Object> map = new HashMap<>();
        map.put("date_calculatedList",date_calculatedList);
        map.put("numDataList",numDataList);
        return map;
    }

    public R countRegister(@PathVariable String day){
        int count = memberService.countRegisterDay(day);
        return R.ok().data("countRegister",count);
    }
}
